package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Visitor;
import com.tanhua.domain.vo.VisitorVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VisitorApiImpl  implements VisitorApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Visitor> findVisitorListByStartDate(long userId, Long startDate) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("date").gt(startDate));
        query.with(Sort.by(Sort.Order.desc("date")));
        return mongoTemplate.find(query, Visitor.class);
    }

    @Override
    public List<Visitor> findVisitorListByCount(long userId, int n) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("date")));
        query.limit(n);
        return mongoTemplate.find(query, Visitor.class);
    }
}
